package admin_users

import (
	"encoding/json"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/credis"
	"github.com/go-redis/redis/v8"
	"strconv"
	"time"
)

var CacheKeyPrefix = TableName + "_"

// @title GetItemFromCacheById
// @param dc *model.DbContext
// @param id int64 数据的id
// @return *AdminUsers
func GetItemFromCacheById(id int64) (*AdminUsers, error) {
	key := CacheKeyPrefix + strconv.FormatInt(id, 10)
	redisOp := credis.New().Tag(TableName)
	data, errGet := redisOp.TGet(key)
	if errGet != nil {
		if errGet == redis.Nil {
			data, errDb := GetItemById(id)
			if errDb != nil {
				return nil, app.NewError(errDb)
			}

			jsonData, _ := json.Marshal(data)

			_, errSet := redisOp.TSet(key, jsonData)
			if errSet != nil {
				panic(errGet)
			}

			return data, nil
		}
		panic("Redis-" + errGet.Error())
	}

	var returnData *AdminUsers
	jsonErr := json.Unmarshal(data, &returnData)
	if jsonErr != nil {
		return nil, app.NewError(jsonErr)
	}

	return returnData, nil
}

// @title GetTokenFromCacheById
// @param id int64 数据的id
// @return string
func GetTokenFromCacheById(id int64) (string, error) {
	key := CacheKeyPrefix + "token_" + strconv.FormatInt(id, 10)
	redisOp := credis.New()
	data, errGet := redisOp.Get(key)
	if errGet != nil {
		if errGet == redis.Nil {
			return "", nil
		}

		return "", errGet
	}

	return string(data), nil
}

// @title 设置用户的token
// @param id int64 数据的id
// @return string
func SetTokenCacheById(id int64, token string, expiration time.Duration) (bool, error) {
	key := CacheKeyPrefix + "token_" + strconv.FormatInt(id, 10)
	redisOp := credis.New()
	errSet := redisOp.Set(key, token, expiration)
	if errSet != nil {
		return false, errSet
	}
	return true, nil
}

// @title GetChildrenFromCacheById
// @description 从缓存中获取管理员的所有下级用户,　可以是仅仅下级或者所有下级
func GetChildrenFromCacheById(adminUserId int64, isDirectChild ...bool) (map[int64]*AdminUsers, error) {
	var key string
	isDirect := false
	if len(isDirectChild) > 0 && isDirectChild[0] {
		isDirect = true
		key = CacheKeyPrefix + "child_direct_" + strconv.FormatInt(adminUserId, 10)
	} else {
		key = CacheKeyPrefix + "child_" + strconv.FormatInt(adminUserId, 10)
	}

	redisOp := credis.New().Tag(TableName)
	data, errGet := redisOp.TGet(key)
	if errGet != nil {
		if errGet == redis.Nil {
			data, errDb := GetChildrenByIdFromDb(adminUserId, isDirect)
			if errDb != nil {
				return nil, app.NewError(errDb)
			}

			jsonData, _ := json.Marshal(data)

			_, errTSet := redisOp.TSet(key, jsonData)
			if errTSet != nil {
				return nil, errTSet
			}

			return data, nil
		}
		panic("Redis-" + errGet.Error())
	}

	var returnData map[int64]*AdminUsers
	jsonErr := json.Unmarshal(data, &returnData)
	if jsonErr != nil {
		return nil, app.NewError(jsonErr)
	}

	return returnData, nil
}

// @title DeleteById
// @description 刷新指定管理员　通过id
func DeleteCacheById(id int64) (bool, error) {
	redisOp := credis.New().Tag(TableName)
	key := CacheKeyPrefix + strconv.FormatInt(id, 10)
	_, err := redisOp.TDel(redisOp.GetTag(), key)
	if err != nil {
		return false, err
	}

	return true, nil
}

// @title DeleteById
// @description 刷新指定用户的 下级用户 1. 直接下级　2. 所有下级
func DeleteChildrenCacheById(id int64) (bool, error) {
	redisOp := credis.New().Tag(TableName)
	key := CacheKeyPrefix + "child_" + strconv.FormatInt(id, 10)
	_, err := redisOp.TDel(redisOp.GetTag(), key)
	if err != nil {
		return false, err
	}

	keyDirect := CacheKeyPrefix + "child_direct_" + strconv.FormatInt(id, 10)
	_, errDirect := redisOp.TDel(redisOp.GetTag(), keyDirect)
	if errDirect != nil {
		return false, errDirect
	}

	return true, nil
}

// @title DeleteTokenCacheById
// @description 刷新指定的用户的token
func DeleteTokenCacheById(id int64) (bool, error) {
	redisOp := credis.New()
	key := CacheKeyPrefix + "token_" + strconv.FormatInt(id, 10)
	_, err := redisOp.Del(key)
	if err != nil {
		return false, err
	}

	return true, nil
}
